
package com.viweb.storemanager.core;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.border.Border;
import sun.swing.DefaultLookup;

public class MyListCellRenderer extends DefaultListCellRenderer {

    private Color oddLine = new Color(240, 240, 255);
    private Color evenLine = Color.WHITE;
    private Color selectedLine = new Color(172, 210, 255);
    private int rowHeight = -1;

    public MyListCellRenderer() {
        super();
    }

    public MyListCellRenderer(int rowHeight) {
        super();
        this.rowHeight = rowHeight;
    }

    /*
     * This method finds the image and text corresponding
     * to the selected value and returns the label, set up
     * to display the text and image.
     */
    @Override
    public Component getListCellRendererComponent(
            JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean hasFocus) {

        if (isSelected) {
            this.setBackground(this.selectedLine);
        } else {
            if (index % 2 == 0) {
                this.setBackground(this.evenLine);
            } else {
                this.setBackground(this.oddLine);
            }
        }

        Border border = null;
        if (hasFocus) {
            if (isSelected) {
                border = DefaultLookup.getBorder(this, ui, "List.focusSelectedCellHighlightBorder");
            }
            if (border == null) {
                border = DefaultLookup.getBorder(this, ui, "List.focusCellHighlightBorder");
            }
        } else {
            border = noFocusBorder;
        }
        this.setBorder(border);

        this.setEnabled(list.isEnabled());

        Dimension size;
        if (this.rowHeight == -1) {
            size = new Dimension(this.getPreferredSize().width, list.getFixedCellHeight());
        } else {
            size = new Dimension(this.getPreferredSize().width, this.rowHeight);
        }
        this.setPreferredSize(size);
        this.setFont(list.getFont());
        this.setForeground(Color.BLACK);
        if (value != null) {
            this.setText(" " + value.toString() + " ");
        } else {
            this.setText(null);
        }

        return this;
    }
}
